package com.sample.generator.config.impl;

import java.io.IOException;
import java.util.Properties;

import com.sample.generator.config.TemplateConfig;
import com.sample.generator.io.InputStreamProvider;
import com.sample.generator.io.ResourceInputStreamProvider;
import com.sample.generator.util.GeneratorUtil;
import com.sample.generator.util.PropertiesLoader;

public abstract class AbstractTemplateConfig implements TemplateConfig {

	private String name;

	private String encoding;

	protected AbstractTemplateConfig(String name) {

		this(name, GeneratorUtil.getSystemDefaultEncoding());
	}

	protected AbstractTemplateConfig(String name, String encoding) {

		if (name == null) {
			throw new IllegalArgumentException("Argument name is null.");
		}
		if (encoding == null) {
			throw new IllegalArgumentException("Argument encoding is null.");
		}
		this.name = name;
		this.encoding = encoding;
	}

	public String getName() {

		return name;
	}

	public String getEncoding() {

		return encoding;
	}

	public Properties getProperties() throws IOException {

		InputStreamProvider provider = new ResourceInputStreamProvider(
				getClass());
		PropertiesLoader loader = new PropertiesLoader(provider);
		String resourceName = getResourceName();
		return loader.loadProperties(resourceName);
	}

	protected String getTemplateType() {

		String templateType = getClass().getSimpleName();
		return templateType;
	}

	protected String getResourceName() {

		String templateType = getTemplateType();
		String resourceName = templateType + ".properties";
		return resourceName;
	}
}
